package com.hui.controller;

import com.hui.entity.Course;
import com.hui.entity.CourseVo;
import com.hui.entity.ResponseResult;
import com.hui.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/08/05/10:11
 * @Description:
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    // 条件查询Course信息
    @RequestMapping(value = "/findAllCourse", method = RequestMethod.POST)
    public ResponseResult findAllCourse(@RequestBody CourseVo courseVo) {
        List<Course> courseList = courseService.getCourseByCondition(courseVo);
        return new ResponseResult(true, 200, "响应成功", courseList);
    }

    // 图片上传
    @RequestMapping(value = "/courseUpload", method = RequestMethod.POST)
    public ResponseResult courseUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        if (file != null) {
            String originalFilename = file.getOriginalFilename();
            // 根据时间戳构建新的文件名
            String newFileName = System.currentTimeMillis() + originalFilename;
            // 获取当前路径 "C:\apache-tomcat-8.5.55\webapps\ssm-web"
            String realPath = request.getServletContext().getRealPath("/");
            // 去掉后面的项目名  "C:\apache-tomcat-8.5.55\webapps\"
            String path = realPath.substring(0, realPath.indexOf("ssm-web"));
            // 文件上传
            file.transferTo(new File(path + "\\upload\\" + newFileName));

            // 将文件相关信息返回出去
            Map<String, String> map = new HashMap<>();
            map.put("fileName", newFileName);// 文件名
            map.put("filePath", "http://localhost:8080/upload/" + newFileName); // 直接访问路径
            return new ResponseResult(true, 200, "响应成功", map);
        }
        return null;
    }

    // 新增或修改Course
    @RequestMapping(value = "/saveOrUpdateCourse", method = RequestMethod.POST)
    public ResponseResult saveOrUpdateCourse(@RequestBody CourseVo courseVo) throws InvocationTargetException, IllegalAccessException {
        if(courseVo.getId()==0){
            courseService.saveCourse(courseVo);
            return new ResponseResult(true, 200, "添加成功", null);
        }else {
            courseService.updateCourse(courseVo);
            return new ResponseResult(true,200,"修改成功",null);
        }
    }

    // 根据id查询课程信息
    @RequestMapping("/findCourseById")
    public ResponseResult findCourseById(Integer id) {
        CourseVo courseVo = courseService.getCourseContentById(id);
        return new ResponseResult(true, 200, "响应成功", courseVo);
    }

    // 跟新 course 状态
    @RequestMapping("/updateCourseStatus")
    public ResponseResult updateCourseStatus(Integer status,Integer id){
        courseService.updateStatus(status,id);
        Map<String,Integer> map = new HashMap<>();
        map.put("status",status);
        return new ResponseResult(true,200,"更新成功",map);

    }

}
